<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="flex justify-between items-center">
        <div class="left">
          <el-button type="success" @click="handleAdd">新增</el-button>
        </div>
        <div class="right">
          <!-- <el-date-picker
            class="mr-4"
            clearable
            v-model="date_value"
            type="daterange"
            range-separator="至"
            start-placeholder="创建开始时间"
            end-placeholder="创建结束时间"
            value-format="yyyy-MM-dd"
            @change="handleChangeDate"
          >
          </el-date-picker> -->
          <el-input
            v-model="listQuery.search"
            clearable
            style="width:200px"
            class="mr-4"
            placeholder="请输入角色名称"
          ></el-input>
          <el-select
            filterable
            v-model="listQuery.status"
            clearable
            class="mr-4"
            placeholder="请选择角色状态"
          >
            <el-option label="正常" :value="1"> </el-option>
            <el-option label="禁用" :value="2"> </el-option>
          </el-select>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </div>
      </div>
      <el-table :data="menuList" border>
        <el-table-column
          prop="role_id"
          label="角色ID"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="role_name"
          label="角色名称"
          width="250"
        ></el-table-column>
        <el-table-column prop="role_memo" label="角色描述"></el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="200"
        ></el-table-column>
        <!-- <el-table-column prop="id" label="操作人"></el-table-column> -->
        <el-table-column prop="" label="状态" width="80">
          <template slot-scope="{ row }">
            <el-tag v-if="row.status === 1" type="success">启用中</el-tag>
            <el-tag v-else type="warning">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type="primary"
              plain
              @click="handleEdit(row.role_id)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              plain
              @click="handleDelete(row.role_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <cus-pagination
        :total="meta.total"
        :page.sync="meta.page"
        :limit.sync="meta.page_size"
        @pagination="handleGetData"
      />
    </el-card>

    <EditComp
      :dialogVisible.sync="dialogVisible"
      :titleStatus.sync="titleStatus"
      :rowId="rowId"
      :data="data"
      @refresh="handleRefresh"
    ></EditComp>
  </div>
</template>

<script>
import EditComp from "./comps/editComp/editComp";

import { getData, getAccessData, deleteData } from "@/api/config/roleList";

export default {
  name: "",
  data() {
    return {
      date_value: null,
      listQuery: {
        search: "",
        status: undefined
      },
      meta: {
        page: 1,
        page_size: 10,
        total: 0
      },
      menuList: null,

      dialogVisible: false,
      titleStatus: "add",
      rowId: 0,
      data: null
    };
  },

  components: {
    EditComp
  },

  computed: {},

  created() {
    this.handleGetData();
    this.handleGetAccessData();
  },

  methods: {
    async handleGetData() {
      const res = await getData(this.listQuery, this.meta);
      this.menuList = res.data.list;
      this.meta.total = res.data.meta.total;
    },
    handleSearch() {
      this.meta.page = 1;
      this.handleGetData();
    },
    handleReset() {
      this.listQuery = {
        search: "",
        status: undefined
      };
      this.meta = {
        page: 1,
        page_size: 10,
        total: 0
      };
      this.handleGetData();
    },
    /**自定义时间 */
    handleChangeDate(value) {
      // if (value) {
      //   this.listQuery.valid_date_start = value[0];
      //   this.listQuery.valid_date_end = value[1];
      // } else {
      //   this.listQuery.valid_date_start = undefined;
      //   this.listQuery.valid_date_end = undefined;
      // }
    },
    handleAdd() {
      this.titleStatus = "add";
      this.dialogVisible = true;
    },
    handleEdit(id) {
      this.rowId = parseInt(id);
      this.titleStatus = "edit";
      this.dialogVisible = true;
    },
    handleRefresh() {
      this.handleGetData();
    },
    handleDelete(id) {
      this.$confirm("确认删除!?此步骤无法恢复", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let res = await deleteData(id);
          if (res.code === 200) {
            this.handleGetData();
            this.$message.success("操作成功");
          }
        })
        .catch(e => {});
    },
    async handleGetAccessData() {
      const res = await getAccessData(1);
      this.data = res.data;
    }
  }
};
</script>
<style lang="scss" scoped></style>
